<?php
/**
 * Created by PhpStorm.
 * User: t.young
 * Date: 2018/6/8
 * Time: 15:17
 */

namespace backend\service;


use backend\component\BackendBaseService;
use backend\component\ResultEasyUIFormat;
use backend\component\ResultFormat;
use common\models\base\Complaint;
use common\models\info\Article;
use common\models\info\Comment;
use common\models\member\User;
use common\models\member\UserAuthor;
use common\models\messager\Notify;

class UserService extends BackendBaseService
{
    /**
     * 返回后台管理员数据列表
     * @param $post
     * @return mixed
     */
    public static function userListData($post) {
        $model = new User();
        $table =  $model->generateListData($post);
        return ResultEasyUIFormat::tableFormat($table)->asJson();
    }

    public static function userAuthListData($post) {
        $model = new User();
        $post["auth_status"] = !isset($post["auth_status"])||$post["auth_status"]==-2?[User::AUTH_STATUS_SUBMISSION,User::AUTH_STATUS_PASS,User::AUTH_STATUS_FAILURE]:$post["auth_status"];
        $table = $model->generateListData($post);
        return ResultEasyUIFormat::tableFormat($table)->asJson();
    }

    /**
     * 获取用户留言评论
     * @param $post
     * @return string
     */
    public static function commentListData($post) {
        $model = new Comment();
        $table =  $model->generateListData($post);

        return ResultEasyUIFormat::tableFormat($table)->asJson();
    }

    public static function commentDataAddArticle($data) {
        $data = json_decode($data);
        if ($data->rows) {
            foreach ($data->rows as $k => $val) {
                $target_id = $val->target_type != Comment::TYPE_ARTICLE? Comment::getFieldById($val->target_id,'target_id'):$val->target_id;
                $data->rows[$k]->title = Article::getFieldById($target_id,'title')?:'empty';
                $data->rows[$k]->target_id = $target_id;
            }
        }
        return $data;
    }

    /*
     * 修改用户权限
     */
    public static function modifyUserPower($user_id,$power,$recommend) {
        if (!User::modifyPower($user_id,$power,$recommend)) {
            return ResultFormat::error("修改失败，请稍后再试");
        }
        return ResultFormat::success("更新成功");
    }

    /**
     * 审核用户申请（作者）
     * @param $id
     * @param $post
     */
    public static function authUserApply($id,$post) {
        $result = User::verify($id,$post);
        if (!$result) {
            return ResultFormat::error("审核失败，请稍后再试");
        }
        return ResultFormat::success("审核成功");
    }

    public static function messageList($post) {
        $model = new Notify();
        $table =  $model->generateListData($post);
        return ResultEasyUIFormat::tableFormat($table)->asJson();
    }
    /*
     * 加载用户举报信息
     */
    public static function complaintList($post) {
        $model = new Complaint();
        $table =  $model->generateListData($post);
        return ResultEasyUIFormat::tableFormat($table)->asJson();
    }
}
